# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created: Sun Aug 10 15:07:41 2008
#      by: PyQt4 UI code generator 4.4.2
#
# WARNING! All changes made in this file will be lost!

import Xml, sys, os, settingsUI
from PyQt4 import QtCore, QtGui

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(630,482)
        Dialog.setMinimumSize(QtCore.QSize(630,482))
        Dialog.setMaximumSize(QtCore.QSize(630,482))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("blue.gif"),QtGui.QIcon.Normal,QtGui.QIcon.Off)
        icon.addPixmap(QtGui.QPixmap("blue.gif"),QtGui.QIcon.Normal,QtGui.QIcon.On)
        Dialog.setWindowIcon(icon)
        Dialog.setSizeGripEnabled(False)
        self.groupBox = QtGui.QGroupBox(Dialog)
        self.groupBox.setGeometry(QtCore.QRect(280,20,331,401))
        self.groupBox.setObjectName("groupBox")
        self.pushButton_2 = QtGui.QPushButton(self.groupBox)
        self.pushButton_2.setGeometry(QtCore.QRect(90,160,151,23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton = QtGui.QPushButton(self.groupBox)
        self.pushButton.setGeometry(QtCore.QRect(220,370,101,23))
        self.pushButton.setObjectName("pushButton")
        self.line_2 = QtGui.QFrame(self.groupBox)
        self.line_2.setGeometry(QtCore.QRect(27,180,281,20))
        self.line_2.setFrameShape(QtGui.QFrame.HLine)
        self.line_2.setFrameShadow(QtGui.QFrame.Sunken)
        self.line_2.setObjectName("line_2")
        self.label_5 = QtGui.QLabel(self.groupBox)
        self.label_5.setGeometry(QtCore.QRect(40,210,81,16))
        self.label_5.setObjectName("label_5")
        self.label_6 = QtGui.QLabel(self.groupBox)
        self.label_6.setGeometry(QtCore.QRect(40,250,81,16))
        self.label_6.setObjectName("label_6")
        self.label_7 = QtGui.QLabel(self.groupBox)
        self.label_7.setGeometry(QtCore.QRect(40,290,121,20))
        self.label_7.setObjectName("label_7")
        self.label_8 = QtGui.QLabel(self.groupBox)
        self.label_8.setGeometry(QtCore.QRect(40,330,91,16))
        self.label_8.setObjectName("label_8")
        self.label_9 = QtGui.QLabel(self.groupBox)
        self.label_9.setGeometry(QtCore.QRect(40,30,251,16))
        self.label_9.setObjectName("label_9")
        self.label_10 = QtGui.QLabel(self.groupBox)
        self.label_10.setGeometry(QtCore.QRect(40,90,91,16))
        self.label_10.setObjectName("label_10")
        self.label_11 = QtGui.QLabel(self.groupBox)
        self.label_11.setGeometry(QtCore.QRect(40,130,81,16))
        self.label_11.setObjectName("label_11")
        self.LbackupLocation = QtGui.QLabel(self.groupBox)
        self.LbackupLocation.setGeometry(QtCore.QRect(70,60,191,16))
        self.LbackupLocation.setObjectName("LbackupLocation")
        self.LMemoryUsed = QtGui.QLabel(self.groupBox)
        self.LMemoryUsed.setGeometry(QtCore.QRect(140,90,111,16))
        self.LMemoryUsed.setObjectName("LMemoryUsed")
        self.LMemoryLeft = QtGui.QLabel(self.groupBox)
        self.LMemoryLeft.setGeometry(QtCore.QRect(140,130,171,16))
        self.LMemoryLeft.setObjectName("LMemoryLeft")
        self.LTimeLastBackup = QtGui.QLabel(self.groupBox)
        self.LTimeLastBackup.setGeometry(QtCore.QRect(140,210,150,16))
        self.LTimeLastBackup.setObjectName("LTimeLastBackup")
        self.LTimeNextBackup = QtGui.QLabel(self.groupBox)
        self.LTimeNextBackup.setGeometry(QtCore.QRect(140,250,101,16))
        self.LTimeNextBackup.setObjectName("LTimeNextBackup")
        self.LTimeBackupInterval = QtGui.QLabel(self.groupBox)
        self.LTimeBackupInterval.setGeometry(QtCore.QRect(140,290,111,16))
        self.LTimeBackupInterval.setObjectName("LTimeBackupInterval")
        self.LNbFilesInBackup = QtGui.QLabel(self.groupBox)
        self.LNbFilesInBackup.setGeometry(QtCore.QRect(140,330,91,16))
        self.LNbFilesInBackup.setObjectName("LNbFilesInBackup")
        self.horizontalSlider = QtGui.QSlider(Dialog)
        self.horizontalSlider.setGeometry(QtCore.QRect(50,400,160,21))
        self.horizontalSlider.setMaximum(1)
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setObjectName("horizontalSlider")
        self.line = QtGui.QFrame(Dialog)
        self.line.setGeometry(QtCore.QRect(20,430,591,16))
        self.line.setFrameShape(QtGui.QFrame.HLine)
        self.line.setFrameShadow(QtGui.QFrame.Sunken)
        self.line.setObjectName("line")
        self.progressBar = QtGui.QProgressBar(Dialog)
        self.progressBar.setGeometry(QtCore.QRect(490,450,118,23))
        self.progressBar.setProperty("value",QtCore.QVariant(24))
        self.progressBar.setObjectName("progressBar")
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(20,450,201,16))
        self.label.setObjectName("label")
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(80,350,161,16))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setWeight(75)
        font.setBold(True)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtGui.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(20,380,46,14))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setWeight(75)
        font.setBold(True)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.label_4 = QtGui.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(220,380,46,14))
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setWeight(75)
        font.setBold(True)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.imageLabel = QtGui.QLabel(Dialog)
        self.imageLabel.setGeometry(QtCore.QRect(20,30,241,301))
        self.LogoFrame = QtGui.QFrame(Dialog)
        self.LogoFrame.setGeometry(QtCore.QRect(29,29,231,301))
        self.LogoFrame.setStyleSheet("QWidget {background-image: url(:blue.gif) }")
        self.LogoFrame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.LogoFrame.setFrameShadow(QtGui.QFrame.Raised)
        self.LogoFrame.setObjectName("LogoFrame")
        font = QtGui.QFont()
        font.setPointSize(9)
        font.setWeight(75)
        font.setBold(True)
        self.imageLabel.setFont(font)
        self.imageLabel.setObjectName("imageLabel")
        self.actionEcriredstext = QtGui.QAction(Dialog)
        self.actionEcriredstext.setObjectName("actionEcriredstext")

        self.retranslateUi(Dialog)
        #QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL("clicked()"),Dialog.hide)
        #settingsUI.launch()
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Easy Backup Tool", None, QtGui.QApplication.UnicodeUTF8))
        self.groupBox.setTitle(QtGui.QApplication.translate("Dialog", "Backup Information", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setText(QtGui.QApplication.translate("Dialog", "Change Backup Directory", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Access Settings...", None, QtGui.QApplication.UnicodeUTF8))
        self.label_5.setText(QtGui.QApplication.translate("Dialog", "Last Backup :", None, QtGui.QApplication.UnicodeUTF8))
        self.label_6.setText(QtGui.QApplication.translate("Dialog", "Next Backup :", None, QtGui.QApplication.UnicodeUTF8))
        self.label_7.setText(QtGui.QApplication.translate("Dialog", "Backup Interval :", None, QtGui.QApplication.UnicodeUTF8))
        self.label_8.setText(QtGui.QApplication.translate("Dialog", "Files in Backup :", None, QtGui.QApplication.UnicodeUTF8))
        self.label_9.setText(QtGui.QApplication.translate("Dialog", "The Backup is stored at the following location :", None, QtGui.QApplication.UnicodeUTF8))
        self.label_10.setText(QtGui.QApplication.translate("Dialog", "Memory Used :", None, QtGui.QApplication.UnicodeUTF8))
        self.label_11.setText(QtGui.QApplication.translate("Dialog", "Memory Left :", None, QtGui.QApplication.UnicodeUTF8))
        self.LbackupLocation.setText(QtGui.QApplication.translate("Dialog", "LBackupLocation", None, QtGui.QApplication.UnicodeUTF8))
        self.LMemoryUsed.setText(QtGui.QApplication.translate("Dialog", "LMemoryUsed", None, QtGui.QApplication.UnicodeUTF8))
        self.LMemoryLeft.setText(QtGui.QApplication.translate("Dialog", "LMemoryLeft", None, QtGui.QApplication.UnicodeUTF8))
        self.LTimeLastBackup.setText(QtGui.QApplication.translate("Dialog", "LTimeLastBackup", None, QtGui.QApplication.UnicodeUTF8))
        self.LTimeNextBackup.setText(QtGui.QApplication.translate("Dialog", "LTimeNextBackup", None, QtGui.QApplication.UnicodeUTF8))
        self.LTimeBackupInterval.setText(QtGui.QApplication.translate("Dialog", "LTimeBackupInterval", None, QtGui.QApplication.UnicodeUTF8))
        self.LNbFilesInBackup.setText(QtGui.QApplication.translate("Dialog", "LNbFilesInBackup", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Dialog", "Backup Status and Progression", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("Dialog", "Easy Backup Tool", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("Dialog", "OFF", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setText(QtGui.QApplication.translate("Dialog", "ON", None, QtGui.QApplication.UnicodeUTF8))
        self.actionEcriredstext.setText(QtGui.QApplication.translate("Dialog", "ecriredstext", None, QtGui.QApplication.UnicodeUTF8))
        #self.LbackupLocation.setText(QtGui.QApplication.translate("Dialog", message, None, QtGui.QApplication.UnicodeUTF8))
        
        #QtGui.QApplication.translate()
        #self.connect(self.pushButton, QtCore.SIGNAL("clicked()"),message)
        
def message():
    print 'bonjour'
    #puttext()
    return

def ret():
    list = Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml', 'BackupDir')
    #print list[0]
    memoryUsage(list[0])
   
    #return list[0]

def memoryUsage(path):
    
    send = []
    result=os.popen('dir '+path).read()
    #print result
    truc = result.split('\n')
    res= truc[-3:]
    res.remove('')
    mem = res[0].split(' ')
    mem1 = res[1].split(' ')
    mem_used = mem[-2]
    mem_free = mem1[-3]
    send.append(mem_used)
    send.append(mem_free)
    formated = formatValues(send)
    ui.LMemoryUsed.setText(QtGui.QApplication.translate("Dialog", formated[0], None, QtGui.QApplication.UnicodeUTF8))
    ui.LMemoryLeft.setText(QtGui.QApplication.translate("Dialog", formated[1], None, QtGui.QApplication.UnicodeUTF8))
    
def formatValues(send):
    
    values={0:'Bytes', 1:'KB', 2:'MB', 3:'GB', 4:'TB', 5:'EB'}
    
    result=[]
    
    for items in send:
        val = items.split(',')
        if len(val)>1:
            result.append(val[0]+'.'+val[1]+' '+ values.get(len(val)-1))
        else:
            result.append(val[0]+' '+ values.get(len(val)-1))
            
    return result

def getTimes():
    """"""
    

def puttext():
    import userPref, Xml, scheduler, time
    from time import strftime, strptime
    
    ui.LbackupLocation.setText(QtGui.QApplication.translate("Dialog", ret(), None, QtGui.QApplication.UnicodeUTF8))
    #return 'putext'
    list = Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml', 'BackupDir')
    ui.LbackupLocation.setText(list[0])
    ui.LTimeBackupInterval.setText(userPref.getValue('BackupInterval')+' seconds')
    lastbackup= Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-LastBackup.xml', 'lastbackuptime')
    ui.LTimeLastBackup.setText(str(lastbackup[0]))
    #ui.LTimeNextBackup.setText(str(strptime(ui.LTimeLastBackup.text(), "%d-%m-%Y@%Hh%Mm%Ss")+int(userPref.getValue('BackupInterval'))))
    truc =  strptime(ui.LTimeLastBackup.text(), "%d-%m-%Y@%Hh%Mm%Ss")
    print strftime('%S', truc)
    xx = ui.LTimeLastBackup.text().split('@')
    #tric = QtCore.QTime.fromString("1mm12car00", "m'mm'hcarss")
    #tric.addSecs(3600)
    #print tric
    ui.LNbFilesInBackup.setText(str(countFiles())+' Files in backup')
    
def countFiles():
    import re
    list = Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml', 'BackupDir')
    listBackup=os.listdir(list[0])
    listmagic=[]
    for elements in listBackup:
        pattern = '(xdelta$|acl$)'
        if not(re.search(pattern, elements)):
            listmagic.append(elements)
    return len(listmagic)

def imageViewer():
    printer = QtGui.QPrinter()
    scaleFactor = 1.0
    imageLabel = QtGui.QLabel()
    ui.imageLabel.setBackgroundRole(QtGui.QPalette.Base)
    ui.imageLabel.setSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Ignored)
    ui.imageLabel.setScaledContents(True)
    
    #fileName = QtGui.QFileDialog.getOpenFileName(self, self.tr("Open File"),
     #                                                QtCore.QDir.currentPath())
    image = QtGui.QImage('LifeJack.jpg')#fileName)
    if image.isNull():
                QtGui.QMessageBox.information(self, self.tr("Image Viewer"),
                                              self.tr("Cannot load %1.").arg(fileName))
                return
    ui.imageLabel.setPixmap(QtGui.QPixmap.fromImage(image))
    scaleFactor = 1.0

def launchSettings():
    settingsUI.launch()
    
def changeDir():
    
    dirdialog = QtGui.QFileDialog(Dialog,"Export Package")
    dirdialog.setFileMode(QtGui.QFileDialog.DirectoryOnly)
    dirdialog.show()
    if (dirdialog.exec_()):
       fileNames = dirdialog.selectedFiles()
    for elem in fileNames:
        print str(elem)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    puttext()
    imageViewer()
    #launchSettings()
    app.connect(ui.pushButton, QtCore.SIGNAL("clicked()"),launchSettings)
    app.connect(ui.pushButton_2, QtCore.SIGNAL("clicked()"),changeDir)
    #app.connect(ui.pushButton, QtCore.SIGNAL("clicked()"),QtCore.SLOT(puttext()))
    
    sys.exit(app.exec_())

